function dlTabsPrefsFilters()
{
	// The tree
	this.filterRoot = document.getElementById("dlTabs_filterRoot");
	// The treechildren
	this.filterTree = document.getElementById("dlTabs_filterTree");
	
	// Save the preferences
	this.savePrefs = function()
	{
		dlTabsPrefs.setNumFilters(this.filterTree.childNodes.length);
		for(var i = 0; i < this.filterTree.childNodes.length; i++)
		{
			var row = this.filterTree.childNodes[i].firstChild;
			if(row)
			{
				var regexcell = row.childNodes[0];
				var timecell = row.childNodes[1];
				
				dlTabsPrefs.setFilterRegex(i, regexcell.getAttribute("label"));
				dlTabsPrefs.setFilterTime(i, timecell.getAttribute("label"));
			}
		}
	}

	// Setup the list of filters
	this.setupFilterList = function()
	{
		var num = dlTabsPrefs.getNumFilters();
		for(var i = 0; i < num; i++)
		{
			var regex = dlTabsPrefs.getFilterRegex(i);
			var time = dlTabsPrefs.getFilterTime(i);
			this.addToTree(regex, time);
		}
	}

	// Add a row to the tree
	this.addToTree = function(regex, time)
	{
		var treeitem = document.createElement("treeitem");
		var treerow = document.createElement("treerow");
		
		var regexcell = document.createElement("treecell");
		regexcell.setAttribute("label", regex);
		var timecell = document.createElement("treecell");
		timecell.setAttribute("label", time);
		
		treerow.appendChild(regexcell);
		treerow.appendChild(timecell);
		
		treeitem.appendChild(treerow);
		
		this.filterTree.appendChild(treeitem);
	}

	// Get the currently selected item in the tree
	this.getSelectedItem = function()
	{
		var index = this.filterRoot.view.selection.currentIndex
		return(this.filterTree.childNodes[index]);
	}

	// Display the filter dialog
	this.displayDialog = function(regex, time, action)
	{
		var params = {inn:{regexparam:regex, timeparam:time, actionparam:action}, out:null}; 
		
		window.openDialog("chrome://downloadtabs/content/filterDialog.xul", "",
							"chrome, dialog, modal, resizable=yes", params).focus();
		if(params.out)
		{
			return(params.out);
		}
	}

	// Move the currently selected item up
	this.moveUp = function()
	{
		var item = this.getSelectedItem();
		if(item && item.previousSibling)
		{
			this.filterTree.insertBefore(item, item.previousSibling);
			
			if(dlTabsPrefsDialog.prefs.instantApply)
				this.savePrefs();
		}
	}

	// Move the currently selected item down
	this.moveDown = function()
	{
		var item = this.getSelectedItem();
		if(item && item.nextSibling)
		{
			this.filterTree.insertBefore(item.nextSibling, item);
			
			if(dlTabsPrefsDialog.prefs.instantApply)
				this.savePrefs();
		}
	}

	// Add an item
	this.add = function()
	{
		var params = this.displayDialog("", 0, "Add");
		
		if(params)
		{
			this.addToTree(params.regex, params.time);
			
			if(dlTabsPrefsDialog.prefs.instantApply)
				this.savePrefs();
		}
	}

	// Edits the currently selected item
	this.edit = function()
	{
		var item = this.getSelectedItem();
		if(item && item.firstChild)
		{
			var row = item.firstChild.childNodes;
			var params = this.displayDialog(row[0].getAttribute("label"), 
									parseInt(row[1].getAttribute("label")), "Edit");
			
			if(params)
			{
				row[0].setAttribute("label", params.regex);
				row[1].setAttribute("label", params.time);
			
				if(dlTabsPrefsDialog.prefs.instantApply)
					this.savePrefs();
			}
		}
	}

	// Remove the currently selected item
	this.remove = function()
	{
		var item = this.getSelectedItem();
		if(item)
		{
			this.filterTree.removeChild(item);
			
			if(dlTabsPrefsDialog.prefs.instantApply)
				this.savePrefs();
		}
	}
	
	// Setup the tree
	this.setupFilterList();
	
	// Add a listener to the OK button or page navigating away to save prefs
	var ok = document.documentElement.getButton("accept");
	if(ok)
	{
		ok.addEventListener("click",
			function(e)
			{
				dlTabsPrefsDialog.filter.savePrefs();
			},
			false);
	}
	else
	{
		window.addEventListener("unload", 
			function(e)
			{
				dlTabsPrefsDialog.filter.savePrefs();
			},
			false);
	}
}